package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController //@controller + @ResponseBody
@CrossOrigin    //跨域
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 作业1: http://localhost:8090/user/findAll
     */
    @GetMapping("/findAll")
    public List<User> findAll(){

        return userService.findAll();
    }

    /**
     * 完成用户新增操作
     * url:http://localhost:8090/user/春节/2022/女
     */
    @GetMapping("/{name}/{age}/{sex}")
    public String saveUser(User user){

        userService.saveUser(user);
        return "新增用户成功!!!";
    }

    /**
     * 完成用户删除操作:
     *      http://localhost:8090/user/deleteUserById?id=1
     */
    @GetMapping("/deleteUserById")
    public String deleteUserById(Integer id){
        userService.deleteUserById(id);
        return "删除成功";
    }

    /**
     * 修改用户数据:
     * http://localhost:8090/user/update/元宵节/2022/男/id号xx
     */
    @GetMapping("/update/{name}/{age}/{sex}/{id}")
    public String updateUser(User user){
        userService.updateUser(user);
        return "修改成功";
    }

    /**
     * http://localhost:8090/user/findUserById?id=xx 查询id=xx的数据 返回User对象
     */
    @GetMapping("/findUserById")
    public User findUserById(Integer id){
        return userService.findUserById(id);
    }

    /**
     * http://localhost:8090/user/findUserBylike?name=xx 模糊查询 返回List集合
     */
    @GetMapping("/findUserBylike")
    public List<User> findUserBylike(String name){
        List<User> userList = userService.findUserBylike(name);
        return userList;
    }

    /**
     * http://localhost:8090/user/findUserByIn?id=1,3,4,5,6 批量查询 返回List集合
     */
    @GetMapping("/findUserByIn")
    public List<User> findUserByIn(Integer[] id){
        List<User> userList1 = userService.findUserByIn(id);
        return userList1;
    }

    /**
     * http://localhost:8090/user/findUserByIn1?minAge=10&maxAge=100 查询数据 返回List集合
     */
   @GetMapping("/findUserByIn1")
    public List<User> findUserByIn1(Integer minAge,Integer maxAge){
        List<User> userList2 = userService.findUserByIn1(minAge,maxAge);
        return userList2;
    }
//    @GetMapping("/findUserByIn1")
//    public List<User> findUserByIn1(Integer minAge,Integer maxAge){
//        HashMap<Object, Integer> map = new HashMap<>();
//        map.put("minAge",minAge);
//        map.put("maxAge",maxAge);
//        List<User> userList2 = userService.findUserByIn1(map);
//        return userList2;
//    }

}

